package com.woniuxy.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @author 佘赛飞
 * @create 2021 -09 -30 10:32 PM
 */
public class MyBatisUtil {

    private static SqlSessionFactory SqlSessionFactory= null;

    public  static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
        try {
            if(SqlSessionFactory==null){
                InputStream inputStream =
                        Resources.getResourceAsStream("mybatisconfig.xml");
                SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            }
            sqlSession=SqlSessionFactory.openSession();
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
        return  sqlSession;

    }

}
